package com.ruiysoft.service.logistics.impl;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.ruiysoft.conf.mq.LogisticsMqConfig;
import com.ruiysoft.entity.logistics.APILogisticsMessage;
import com.ruiysoft.entity.logistics.LogisticsSubscribe;
import com.ruiysoft.entity.logistics.kuaidi100.CallbackMessage;
import com.ruiysoft.entity.logistics.kuaidi100.CallbackMessageTool;
import com.ruiysoft.service.logistics.ILogisticsCallback;
import com.ruiysoft.service.logistics.ILogisticsQueryService;
import com.ruiysoft.service.order.IOrderInnerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 *
 */
@Component
public class LogisticsCallBackImpl implements ILogisticsCallback {

    private static Logger logger = LoggerFactory.getLogger(LogisticsCallBackImpl.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ILogisticsQueryService iLogisticsQueryService;

    @Autowired
    private IOrderInnerService orderInnerService;

    @Override
    public void push2MQ(String apiSupplier, Long shopID, String param) {
        ILogisticsCallback.LogisticsCallBackMQVO mqvo = createMQVO(apiSupplier, shopID, param);
        rabbitTemplate.convertAndSend(LogisticsMqConfig.BACKCALL_EXCHANGE_NAME,LogisticsMqConfig.BACKCALL_ROUTE_KEY,mqvo);
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = LogisticsMqConfig.BACKCALL_QUEUE_NAME, durable = "true", autoDelete = "false"),
                    exchange = @Exchange(value = LogisticsMqConfig.BACKCALL_EXCHANGE_NAME, type = ExchangeTypes.DIRECT),
                    key = LogisticsMqConfig.BACKCALL_ROUTE_KEY)
    )
    public void onMessage(Channel channel, Message message) {
        try {
            ILogisticsCallback.LogisticsCallBackMQVO mqvo = JSON.parseObject(message.getBody(),ILogisticsCallback.LogisticsCallBackMQVO.class);
            CallbackMessage callbackMessage = CallbackMessageTool.toCallbackMessage(mqvo.getParam());
            APILogisticsMessage logisticsMessage = CallbackMessageTool.toAPILogisticsMessage(callbackMessage);

            if(logger.isInfoEnabled()) {
                logger.info("物流回调消息队列,ApiSupplier:{},ShopID:{},ExpressNO:{}",mqvo.getApiSupplier(),mqvo.getShopID(),logisticsMessage.getExpressNO());
            }

            iLogisticsQueryService.updateLogistics(mqvo.getApiSupplier(), mqvo.getShopID(), logisticsMessage);
            if (logisticsMessage.getIscheck()) {
                List<LogisticsSubscribe> list = iLogisticsQueryService.getSubscribe(logisticsMessage.getExpressNO(), logisticsMessage.getComCode());
                if (list != null) {
                    for (LogisticsSubscribe subscribe : list) {
                        orderInnerService.sign4Order(subscribe.getSubscribeId());
                    }
                }
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e){
            logger.error("队列消费出错：物流回调队列");
        }

    }

}
